package com.encryption.rpc.facade
{
	import com.encryption.feature.user.GlobalUser;
	import com.encryption.rpc.RPCEvent;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class FileFacade
	{
		public static const OPERATION_UPLOAD:String = "upload";
		public static const OPERATION_getFilePagingByCondition:String = "getFilePagingByCondition";
		public static const OPERATION_DETETE_FILE:String = "deleteFile";
		public static const OPERATION_getFileByCondition:String = "getFileByCondition";
		
		private var remoteObject:RemoteObject;
		private var handler:Function;
		private var argsObj:Object;
		
		public function FileFacade(operation:String,argsObj:Object,handler:Function){
			remoteObject = new RemoteObject();
			remoteObject.destination = "fileFacade";
			this.handler = handler;
			this.argsObj = argsObj;
			
			if(operation == OPERATION_UPLOAD){
				upload();
			}else if(operation == OPERATION_getFilePagingByCondition){
				getFilePagingByCondition();
			}else if(operation == OPERATION_DETETE_FILE){
				deleteFile();
			}else if(operation == OPERATION_getFileByCondition){
				getFileByCondition();
			}
		}
		
		private function upload():void{
			remoteObject.upload(argsObj.arg0, argsObj.arg1, argsObj.arg2);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function getFilePagingByCondition():void{
			remoteObject.getFilePagingByCondition(argsObj.arg0, GlobalUser.currentUser, 0, 15);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function deleteFile():void{
			remoteObject.deleteFile(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function getFileByCondition():void{
			remoteObject.getFileByCondition(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function onFault(evt:FaultEvent):void{
			var rpcEvent:RPCEvent = new RPCEvent(evt);
			this.handler(rpcEvent);
		}
		
		private function onResult(evt:ResultEvent):void{
			remoteObject.disconnect();
			var rpcEvent:RPCEvent = new RPCEvent(evt);
			this.handler(rpcEvent);
		}
	}
}